<!DOCTYPE html>
<html lang="zh" data-direction="ltr">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta
      name="keywords"
      content="Go-Micro, Micro, 中文, Go-Micro教程, Go-Micro资源"
    />
    <meta
      name="description"
      content="Micro 中国站，只为原生中文资源"
    />
    <title>Micro Runtime - Micro 中国</title>
    <meta data-rh="true" property="og:type" content="website"/><meta data-rh="true" property="og:image" content=""/><meta data-rh="true" property="og:title" content="Micro Runtime - Micro 中国"/><meta data-rh="true" name="description" content="Micro 是开发微服务的工具库"/>
    <link
      rel="icon"
      href="https://avatars2.githubusercontent.com/u/20906580?s=60&v=4"
      type="image/x-icon"
    />
    
    <link rel="stylesheet" type="text/css" href="/index-47dfeecd.css" />
    
    <style id="nprogress-style">
      #nprogress {
        display: none;
      }
    </style>
    <link rel="stylesheet/less" type="text/css" href="/color.less" />
    <script src="https://polyfill.alicdn.com/polyfill.min.js?features=default,es2015,Intl"></script>
    <script>
      (function () {
        function isLocalStorageNameSupported() {
          var testKey = 'test';
          var storage = window.localStorage;
          try {
            storage.setItem(testKey, '1');
            storage.removeItem(testKey);
            return true;
          } catch (error) {
            return false;
          }
        }
        // 优先级提高到所有静态资源的前面，语言不对，加载其他静态资源没意义
        var pathname = location.pathname;

        function isZhCN(pathname) {
          return /-cn\/?$/.test(pathname);
        }
        function getLocalizedPathname(path, zhCN) {
          var pathname = path.startsWith('/') ? path : '/' + path;
          if (!zhCN) {
            // to enUS
            return /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
          } else if (pathname === '/') {
            return '/index-cn';
          } else if (pathname.endsWith('/')) {
            return pathname.replace(/\/$/, '-cn/');
          }
          return pathname + '-cn';
        }

        // 兼容旧的 URL， `?locale=...`
        var queryString = location.search;
        if (queryString) {
          var isZhCNConfig = queryString.indexOf('zh-CN') > -1;
          if (isZhCNConfig && !isZhCN(pathname)) {
            location.pathname = getLocalizedPathname(pathname, isZhCNConfig);
          }
        }

        // 首页无视链接里面的语言设置 https://github.com/ant-design/ant-design/issues/4552
        if (isLocalStorageNameSupported() && (pathname === '/' || pathname === '/index-cn')) {
          var lang =
            (window.localStorage && localStorage.getItem('locale')) ||
            ((navigator.language || navigator.browserLanguage).toLowerCase() === 'zh-cn'
              ? 'zh-CN'
              : 'en-US');
          // safari is 'zh-cn', while other browser is 'zh-CN';
          if ((lang === 'zh-CN') !== isZhCN(pathname)) {
            location.pathname = getLocalizedPathname(pathname, lang === 'zh-CN');
          }
        }
        document.documentElement.className += isZhCN(pathname) ? 'zh-cn' : 'en-us';
      })();
    </script>
  </head>
  <body>
    <div id="react-content">
      <div class="page-wrapper"><header id="header" class="clearfix"><div class="ant-row" style="flex-flow:nowrap;height:64px"><div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-6 ant-col-xl-5 ant-col-xxl-4"><h1><a id="logo" href="/index-cn"><img alt="logo" src=""/>Micro 中国</a></h1></div><div class="ant-col menu-row ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-18 ant-col-xl-19 ant-col-xxl-20"><div id="search-box" class=""><span role="img" aria-label="search" class="anticon anticon-search"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="search" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M909.6 854.5L649.9 594.8C690.2 542.7 712 479 712 412c0-80.2-31.3-155.4-87.9-212.1-56.6-56.7-132-87.9-212.1-87.9s-155.5 31.3-212.1 87.9C143.2 256.5 112 331.8 112 412c0 80.1 31.3 155.5 87.9 212.1C256.5 680.8 331.8 712 412 712c67 0 130.6-21.8 182.7-62l259.7 259.6a8.2 8.2 0 0011.6 0l43.6-43.5a8.2 8.2 0 000-11.6zM570.4 570.4C528 612.7 471.8 636 412 636s-116-23.3-158.4-65.6C211.3 528 188 471.8 188 412s23.3-116.1 65.6-158.4C296 211.3 352.2 188 412 188s116.1 23.2 158.4 65.6S636 352.2 636 412s-23.3 116.1-65.6 158.4z"></path></svg></span><input type="text" placeholder="在 Micro China 中搜索" class="ant-input" value=""/></div><ul class="ant-menu menu-site ant-menu-light ant-menu-root ant-menu-horizontal" id="nav" direction="ltr" role="menu"><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem"><a href="/docs/micro/introduce-cn">文档</a></li><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem"><a href="/components/button-cn/">插件</a></li><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem"><a href="/docs/resources-cn">资源</a></li><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul><button type="button" class="ant-btn header-button header-lang-button ant-btn-sm">繁体中文</button><button type="button" class="ant-btn header-button ant-dropdown-trigger ant-btn-sm">更多<span role="img" aria-label="down" style="font-size:9px;margin-left:2px;vertical-align:middle;margin-top:-1px" class="anticon anticon-down"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></button><span id="github-btn" class="github-btn"><a class="gh-btn" href="//github.com/micro/go-micro/" target="_blank"><span class="gh-ico" aria-hidden="true"></span><span class="gh-text">Star</span></a><a class="gh-count" target="_blank" href="//github.com/micro/go-micro/stargazers/"></a></span></div></div></header><div class="main-wrapper"><div class="ant-row"><div class="ant-col main-menu ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-6 ant-col-xl-5 ant-col-xxl-4"><div><div class=""><section class="main-menu-inner"><ul class="ant-menu aside-container menu-site ant-menu-light ant-menu-root ant-menu-inline" direction="ltr" role="menu"></ul></section></div></div></div><div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-18 ant-col-lg-18 ant-col-xl-19 ant-col-xxl-20"><section class="main-container"><article class="markdown"><div><h1>Micro Runtime<a class="edit-button" href="https://github.com/ant-design/ant-design/edit/master/docs/micro/runtime.zh-CN.md" target="_blank" rel="noopener noreferrer"><span role="img" aria-label="edit" class="anticon anticon-edit"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="edit" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M257.7 752c2 0 4-.2 6-.5L431.9 722c2-.4 3.9-1.3 5.3-2.8l423.9-423.9a9.96 9.96 0 000-14.1L694.9 114.9c-1.9-1.9-4.4-2.9-7.1-2.9s-5.2 1-7.1 2.9L256.8 538.8c-1.5 1.5-2.4 3.3-2.8 5.3l-29.5 168.2a33.5 33.5 0 009.4 29.8c6.6 6.4 14.9 9.9 23.8 9.9zm67.4-174.4L687.8 215l73.3 73.3-362.7 362.6-88.9 15.7 15.6-89zM880 836H144c-17.7 0-32 14.3-32 32v36c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-36c0-17.7-14.3-32-32-32z"></path></svg></span></a></h1></div><div class="toc-affix"><div class=""><ul class="toc"><li><a class="bisheng-toc-h1" href="#概览" title="概览">概览</a></li><li><a class="bisheng-toc-h2" href="#特性" title="特性">特性</a></li><li><a class="bisheng-toc-h2" href="#开始使用" title="开始使用">开始使用</a></li><li><a class="bisheng-toc-h2" href="#安装-Micro" title="安装 Micro">安装 Micro</a></li><li><a class="bisheng-toc-h2" href="#依赖" title="依赖">依赖</a></li><li><a class="bisheng-toc-h2" href="#服务发现" title="服务发现">服务发现</a></li><li><a class="bisheng-toc-h2" href="#Protobuf" title="Protobuf">Protobuf</a></li><li><a class="bisheng-toc-h2" href="#编写服务" title="编写服务">编写服务</a></li><li><a class="bisheng-toc-h2" href="#示例" title="示例">示例</a></li><li><a class="bisheng-toc-h2" href="#相关插件" title="相关插件">相关插件</a></li></ul></div></div><section class="markdown"><p>Micro 是开发微服务的工具库</p><h1 id="概览"><span>概览</span><a href="#概览" class="anchor">#</a></h1><p>Micro 重点工作服务方向是满足构建可伸缩的系统。Micro 包含了微服务的架构模式，并把它把转换成一系列构建平台的工具。Micro 深入复杂的分布式系统，且要把其简化抽象成众所周知的方式暴露给开发者使用。</p><p>技术本身是日新月异的。而那些基础的技术栈也总是会发生变动。Micro 工具插件化就是要处理这个问题。</p><p>Micro 的愿景是，在任何技术栈中都可插拔，使用 Micro 构建永不过时的系统。</p><h2 id="特性"><span>特性</span><a href="#特性" class="anchor">#</a></h2><p>micro 工具库由以下几个部分组成：</p><ul><li><p><strong>API Gateway:</strong> API 网关作为单一的 http 入口，它使用服务发现中查询的服务地址，把请求动态路由到具体服务。网关允许我们建立可伸缩的后台微服务架构，并且让工作在前端的公共 API 更健壮。Micro API 基于服务发现拥有强大的路由能力，它可以处理 http、gRPC、websocket、消息推送事件等等。</p></li><li><p><strong>Interactive CLI:</strong> 交互式的命令行接口。CLI 通过终端可以描述、查询、直接与平台和服务进行交互。CLI 提供所有的命令让开发者明白微服务正在处理的事情。CLI 也包含了交互模式。</p></li><li><p><strong>Service Proxy:</strong> 服务代理，基于<a href="https://github.com/micro/go-micro">Go Micro</a>和<a href="https://github.com/micro/protocol">MUCP 协议</a>构建的透明的代理服务。它将服务发现、负载均衡、消息编码、中间件、传输及代理插件卸载到单一位置。</p></li></ul><p>可以单独运行或与服务一起运行。</p><ul><li><p><strong>Service Templates:</strong> 服务生成模板，目的是快速生成服务代码，让编写代码飞起来。Micro 预置了一些模板用来编写服务。保持相同的方式编写服务，提高效率。</p></li><li><p><strong>SlackOps Bot:</strong> Slack 小机器人插件，当它运行中服务中时，这个插件允许开发者通过 Slack 消息来操作平台。MicroBot 插件提供聊天配置选项，这样就可以让团队通过向小机器人发送聊天消息来做一些我们希望它做的事，这里面当然也包含像动态发现服务一样创建 slack 命令。</p></li><li><p><strong>Web Dashboard:</strong> 通过 Web 管理控制台，可以直接在 Web 页面上查看服务的运行情况，展示端点信息，请求与响应状态，甚至直接向服务进行查询。管理控制台也有 CLI 交互页面提供给开发者在线上处理，就像直接操作终端一样。</p></li></ul><h2 id="开始使用"><span>开始使用</span><a href="#开始使用" class="anchor">#</a></h2><ul><li><p><a href="#安装Micro">安装 Micro</a></p></li><li><p><a href="#依赖">依赖</a></p></li><li><p><a href="#服务发现">服务发现</a></p></li><li><p><a href="#编写服务">编写服务</a></p></li><li><p><a href="#示例">示例</a></p></li><li><p><a href="#相关插件">相关插件</a></p></li></ul><h2 id="安装-Micro"><span>安装 Micro</span><a href="#安装-Micro" class="anchor">#</a></h2><pre class="language-shell"><code>go get <span class="token operator">-</span>u github<span class="token punctuation">.</span>com<span class="token operator">/</span>micro<span class="token operator">/</span>micro</code></pre><p>也可以通过 Docker 安装</p><pre class="language-shell"><code>docker pull micro<span class="token operator">/</span>micro</code></pre><h2 id="依赖"><span>依赖</span><a href="#依赖" class="anchor">#</a></h2><p>micro 工具库有两个依赖：</p><ul><li><p><a href="#service-discovery">Service Discovery</a> - 用于服务名解析</p></li><li><p><a href="#protobuf">Protobuf</a> - 代码生成工具</p></li></ul><h2 id="服务发现"><span>服务发现</span><a href="#服务发现" class="anchor">#</a></h2><p>服务发现用于服务名解析、路由、集中元数据。</p><p>Micro 使用<a href="https://github.com/micro/go-micro">go-micro</a>来向服务发现中心发送注册请求。</p><h3 id="Consul"><span>Consul</span><a href="#Consul" class="anchor">#</a></h3><p>安装并运行 Consul</p><pre class="language-shell"><code># install
brew install consul

# run
consul agent <span class="token operator">-</span>dev</code></pre><h3 id="mDNS"><span>mDNS</span><a href="#mDNS" class="anchor">#</a></h3><p>Micro 内置了 mDNS 组播系统，这是一种零依赖的服务注册发现机制，它是区别于有注册中心的替代方案。</p><p>通过在启动指令中传入<code>--registry=mdns</code> 或者在<strong>环境变量</strong>中设置<code>MICRO_REGISTRY=mdns</code>。</p><p>其实也可以不传，早期版本的 go-micro 默认注册中心是<strong>consul</strong>，现在换成了<strong>mdns</strong></p><pre class="language-shell"><code># 使用命令行参数
micro <span class="token operator">-</span><span class="token operator">-</span>registry<span class="token operator">=</span>mdns list services

# 使用环境变量
MICRO_REGISTRY<span class="token operator">=</span>mdns micro list services`</code></pre><p>查看更多关于插件的资料：<a href="https://github.com/micro/go-plugins">go-plugins</a>.</p><h2 id="Protobuf"><span>Protobuf</span><a href="#Protobuf" class="anchor">#</a></h2><p>Protobuf 功能是代码生成，可以免去手写一些模板化的代码。</p><pre class="language-null"><code># 安装protobuf
brew install protobuf

# 安装golang的protobuf代码生成器 protoc<span class="token operator">-</span>gen<span class="token operator">-</span>go
go get <span class="token operator">-</span>u github<span class="token punctuation">.</span>com<span class="token operator">/</span>golang<span class="token operator">/</span>protobuf<span class="token operator">/</span>{proto<span class="token punctuation">,</span>protoc<span class="token operator">-</span>gen<span class="token operator">-</span>go}

# 安装micro的protobuf插件 protoc<span class="token operator">-</span>gen<span class="token operator">-</span>micro
go get <span class="token operator">-</span>u github<span class="token punctuation">.</span>com<span class="token operator">/</span>micro<span class="token operator">/</span>protoc<span class="token operator">-</span>gen<span class="token operator">-</span>micro</code></pre><p>更多资料请查看<a href="https://github.com/micro/protoc-gen-micro">protoc-gen-micro</a>。</p><h2 id="编写服务"><span>编写服务</span><a href="#编写服务" class="anchor">#</a></h2><p>上面说过 Micro 是有模板生成功能可以快速生成服务应用。</p><p>详情可以翻阅：<a href="https://github.com/micro/go-micro"><strong>go-micro</strong></a>.</p><h3 id="使用模板生成"><span>使用模板生成</span><a href="#使用模板生成" class="anchor">#</a></h3><p>下面演示使用<code>micro new</code>命令来快速生成一个示例服务</p><p>生成的服务会被放到\$GOPATH 的相对目录下：</p><pre class="language-null"><code>micro new github<span class="token punctuation">.</span>com<span class="token operator">/</span>micro<span class="token operator">/</span>example</code></pre><p>刚这个命令生成的目录如下所示：</p><pre class="language-null"><code>example<span class="token operator">/</span>
	Dockerfile	# A template docker file
	README<span class="token punctuation">.</span>md	# A readme <span class="token keyword">with</span> command used
	handler<span class="token operator">/</span>	# Example rpc handler
	main<span class="token punctuation">.</span>go		# The main Go program
	proto<span class="token operator">/</span>		# Protobuf directory
	subscriber<span class="token operator">/</span>	# Example pubsub Subscriber</code></pre><p>然后使用<code>protoc</code>把 proto 方便生成 go 源码</p><pre class="language-null"><code>protoc <span class="token operator">-</span><span class="token operator">-</span>proto_path<span class="token operator">=</span><span class="token punctuation">.</span> <span class="token operator">-</span><span class="token operator">-</span>micro_out<span class="token operator">=</span><span class="token punctuation">.</span> <span class="token operator">-</span><span class="token operator">-</span>go_out<span class="token operator">=</span><span class="token punctuation">.</span> proto<span class="token operator">/</span>example<span class="token operator">/</span>example<span class="token punctuation">.</span>proto</code></pre><p>下面就可以像运行其它 go 语言程序一下执行下面的命令了：</p><pre class="language-null"><code>go run main<span class="token punctuation">.</span>go</code></pre><h2 id="示例"><span>示例</span><a href="#示例" class="anchor">#</a></h2><p>刚我们通过<code>micro new</code>生成了命令并跑了起来，下面我们测一下。</p><ul><li><p><a href="#list-services">列出所有服务</a></p></li><li><p><a href="#get-service">获取服务信息</a></p></li><li><p><a href="#call-service">调用服务</a></p></li><li><p><a href="#run-api">执行 API</a></p></li><li><p><a href="#call-api">调用 API</a></p></li></ul><h3 id="列出所有服务"><span>列出所有服务</span><a href="#列出所有服务" class="anchor">#</a></h3><p>通过服务发现注册的服务都可以被列出来</p><pre class="language-shell"><code>micro list services</code></pre><p>相关服务：</p><pre class="language-null"><code>consul
go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>srv<span class="token punctuation">.</span>example
topic<span class="token punctuation">:</span>topic<span class="token punctuation">.</span>go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>srv<span class="token punctuation">.</span>example</code></pre><p>示例中的注册了的应用使用的限定了的注册名<code>go.micro.srv.example</code>，<code>go.micro.srv</code>是后台服务名的默认前缀</p><h3 id="获取指定名的服务"><span>获取指定名的服务</span><a href="#获取指定名的服务" class="anchor">#</a></h3><p>每一个服务注册都是通过唯一的 id、地址及元数据。</p><pre class="language-shell"><code>micro get service go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>srv<span class="token punctuation">.</span>example</code></pre><p>输出的结果：</p><pre class="language-null"><code>service  go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>srv<span class="token punctuation">.</span>example

version latest

ID	Address	Port	Metadata
go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>srv<span class="token punctuation">.</span>example<span class="token operator">-</span>437d1277<span class="token operator">-</span>303b<span class="token operator">-</span><span class="token number">11e8</span><span class="token operator">-</span>9be9<span class="token operator">-</span>f40f242f6897	<span class="token number">192.168</span><span class="token punctuation">.</span><span class="token number">1.65</span>	<span class="token number">53545</span>	transport<span class="token operator">=</span>http<span class="token punctuation">,</span>broker<span class="token operator">=</span>http<span class="token punctuation">,</span>server<span class="token operator">=</span>rpc<span class="token punctuation">,</span>registry<span class="token operator">=</span>consul

Endpoint<span class="token punctuation">:</span> Example<span class="token punctuation">.</span>Call
Metadata<span class="token punctuation">:</span> stream<span class="token operator">=</span><span class="token boolean">false</span>

Request<span class="token punctuation">:</span> {
	name string
}

Response<span class="token punctuation">:</span> {
	msg string
}


Endpoint<span class="token punctuation">:</span> Example<span class="token punctuation">.</span>PingPong
Metadata<span class="token punctuation">:</span> stream<span class="token operator">=</span><span class="token boolean">true</span>

Request<span class="token punctuation">:</span> {}

Response<span class="token punctuation">:</span> {}


Endpoint<span class="token punctuation">:</span> Example<span class="token punctuation">.</span>Stream
Metadata<span class="token punctuation">:</span> stream<span class="token operator">=</span><span class="token boolean">true</span>

Request<span class="token punctuation">:</span> {}

Response<span class="token punctuation">:</span> {}


Endpoint<span class="token punctuation">:</span> <span class="token keyword">Func</span>
Metadata<span class="token punctuation">:</span> subscriber<span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">,</span>topic<span class="token operator">=</span>topic<span class="token punctuation">.</span>go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>srv<span class="token punctuation">.</span>example

Request<span class="token punctuation">:</span> {
	say string
}

Response<span class="token punctuation">:</span> {}


Endpoint<span class="token punctuation">:</span> Example<span class="token punctuation">.</span>Handle
Metadata<span class="token punctuation">:</span> subscriber<span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">,</span>topic<span class="token operator">=</span>topic<span class="token punctuation">.</span>go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>srv<span class="token punctuation">.</span>example

Request<span class="token punctuation">:</span> {
	say string
}

Response<span class="token punctuation">:</span> {}</code></pre><h3 id="调用服务"><span>调用服务</span><a href="#调用服务" class="anchor">#</a></h3><p>通过 CLI 命令行接口来调用 RPC 服务，这次查询我们使用 JSON 来发送：</p><pre class="language-shell"><code>micro call go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>srv<span class="token punctuation">.</span>example Example<span class="token punctuation">.</span>Call <span class="token string">'{"name": "John"}'</span></code></pre><p>输出的结果：</p><pre class="language-null"><code>{
	<span class="token string">"msg"</span><span class="token punctuation">:</span> <span class="token string">"Hello John"</span>
}</code></pre><p>可以查看<a href="https://micro.mu/docs/cli.html">cli 文档来查看</a>更多信息。</p><p>下面，我们试一下通过 HTTP 来调用服务。</p><h3 id="执行-API"><span>执行 API</span><a href="#执行-API" class="anchor">#</a></h3><p>Micro API 本质上是一个 http 协议的网关接口，它会把动态路由到转到后台服务中。</p><pre class="language-null"><code>MICRO_API_HANDLER<span class="token operator">=</span>rpc \
MICRO_API_NAMESPACE<span class="token operator">=</span>go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>srv \
micro api</code></pre><p>说明信息：</p><ul><li><p><code>MICRO_API_HANDLER</code> http API 的触发方法</p></li><li><p><code>MICRO_API_NAMESPACE</code> API 所属服务的命名空间 </p></li></ul><h3 id="调用-API"><span>调用 API</span><a href="#调用-API" class="anchor">#</a></h3><p>发送一个 POST 请求到 API：</p><pre class="language-null"><code>curl <span class="token operator">-</span>XPOST <span class="token operator">-</span>H <span class="token string">'Content-Type: application/json'</span> <span class="token operator">-</span>d <span class="token string">'{"name": "John"}'</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token punctuation">:</span><span class="token number">8080</span><span class="token operator">/</span>example<span class="token operator">/</span>call</code></pre><p>结果：</p><pre class="language-null"><code>{<span class="token string">"msg"</span><span class="token punctuation">:</span><span class="token string">"Hello John"</span>}</code></pre><p>更多详情信息翻看<a href="https://micro.mu/docs/api.html">api doc</a>。</p><h2 id="相关插件"><span>相关插件</span><a href="#相关插件" class="anchor">#</a></h2><p>Micro 是基于<a href="https://github.com/micro/go-micro">go-micro</a>来开发插件。</p><p>Go-micro 向分布式系统架构可抽离的基础部分提供抽象。</p><h3 id="可插拔特性"><span>可插拔特性</span><a href="#可插拔特性" class="anchor">#</a></h3><p>下面列表出的 Micro 特性都是可插拔的：</p><ul><li><p>broker - 发布订阅消息的代理</p></li><li><p>registry - 服务发现与注册</p></li><li><p>selector - 客户端负载均衡</p></li><li><p>transport - 传输，请求-响应或者双向流</p></li><li><p>client - 管理以上特性的客户端</p></li><li><p>server - 管理以上特性的服务端</p></li></ul><p>Find plugins at <a href="https://github.com/micro/go-plugins">go-plugins</a></p><h3 id="使用插件"><span>使用插件</span><a href="#使用插件" class="anchor">#</a></h3><p>插件的集成很简单，只需要创建个文件并引用这些插件即可。</p><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token comment" spellcheck="true">// etcd v3 registry</span>
	<span class="token boolean">_</span> <span class="token string">"github.com/micro/go-plugins/registry/etcdv3"</span>
	<span class="token comment" spellcheck="true">// nats transport</span>
	<span class="token boolean">_</span> <span class="token string">"github.com/micro/go-plugins/transport/nats"</span>
	<span class="token comment" spellcheck="true">// kafka broker</span>
	<span class="token boolean">_</span> <span class="token string">"github.com/micro/go-plugins/broker/kafka"</span>
<span class="token punctuation">)</span></code></pre><h3 id="构建二进制文件"><span>构建二进制文件</span><a href="#构建二进制文件" class="anchor">#</a></h3><p>直接使用 Go 命令行工具链即可。</p><pre class="language-shell"><code># 本地打包
go build <span class="token operator">-</span>i <span class="token operator">-</span>o micro <span class="token punctuation">.</span><span class="token operator">/</span>main<span class="token punctuation">.</span>go <span class="token punctuation">.</span><span class="token operator">/</span>plugins<span class="token punctuation">.</span>go

# 打包成docker镜像
CGO_ENABLED<span class="token operator">=</span><span class="token number">0</span> GOOS<span class="token operator">=</span>linux go build <span class="token operator">-</span>a <span class="token operator">-</span>installsuffix cgo <span class="token operator">-</span>ldflags <span class="token string">'-w'</span> <span class="token operator">-</span>i <span class="token operator">-</span>o micro <span class="token punctuation">.</span><span class="token operator">/</span>main<span class="token punctuation">.</span>go <span class="token punctuation">.</span><span class="token operator">/</span>plugins<span class="token punctuation">.</span>go</code></pre><h3 id="激活插件"><span>激活插件</span><a href="#激活插件" class="anchor">#</a></h3><p>引用插件后如果要激活，那么可以使用命令行或者设置环境变量即可。</p><pre class="language-shell"><code># 命令行参数
micro <span class="token operator">-</span><span class="token operator">-</span>registry<span class="token operator">=</span>etcdv3 <span class="token operator">-</span><span class="token operator">-</span>transport<span class="token operator">=</span>nats <span class="token operator">-</span><span class="token operator">-</span>broker<span class="token operator">=</span>kafka <span class="token punctuation">[</span>其它命令<span class="token punctuation">]</span>

# 环境变量
MICRO_REGISTRY<span class="token operator">=</span>etcdv3 MICRO_TRANSPORT<span class="token operator">=</span>nats MICRO_BROKER<span class="token operator">=</span>kafka micro <span class="token punctuation">[</span>其它命令<span class="token punctuation">]</span></code></pre></section><section class="markdown api-container"></section></article><div class="contributors-list" style="display:flex;list-style:none;margin:0;padding:0"><span style="opacity:0.3" class="ant-avatar ant-avatar-circle"><span class="ant-avatar-string" style="opacity:0"></span></span></div></section><section class="prev-next-nav"></section><footer class="rc-footer rc-footer-dark"><section class="rc-footer-container"><section class="rc-footer-columns"><div class="rc-footer-column"><h2>相关资源</h2><div class="rc-footer-item"><a href="https://github.com/micro-in-cn/platform-web" target="_blank">Platform Web 治理平台</a></div><div class="rc-footer-item"><a href="https://github.com/micro-in-cn/XConf" target="_blank">XConf 配置中心</a></div></div><div class="rc-footer-column"><h2>社区</h2><div class="rc-footer-item"><a href="https://github.com/websemantics/awesome-ant-design" target="_blank"><span class="rc-footer-item-icon"><span role="img" aria-label="comment" class="anticon anticon-comment"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="comment" width="1em" height="1em" fill="currentColor" aria-hidden="true"><defs><style></style></defs><path d="M573 421c-23.1 0-41 17.9-41 40s17.9 40 41 40c21.1 0 39-17.9 39-40s-17.9-40-39-40zm-280 0c-23.1 0-41 17.9-41 40s17.9 40 41 40c21.1 0 39-17.9 39-40s-17.9-40-39-40z"></path><path d="M894 345a343.92 343.92 0 00-189-130v.1c-17.1-19-36.4-36.5-58-52.1-163.7-119-393.5-82.7-513 81-96.3 133-92.2 311.9 6 439l.8 132.6c0 3.2.5 6.4 1.5 9.4a31.95 31.95 0 0040.1 20.9L309 806c33.5 11.9 68.1 18.7 102.5 20.6l-.5.4c89.1 64.9 205.9 84.4 313 49l127.1 41.4c3.2 1 6.5 1.6 9.9 1.6 17.7 0 32-14.3 32-32V753c88.1-119.6 90.4-284.9 1-408zM323 735l-12-5-99 31-1-104-8-9c-84.6-103.2-90.2-251.9-11-361 96.4-132.2 281.2-161.4 413-66 132.2 96.1 161.5 280.6 66 412-80.1 109.9-223.5 150.5-348 102zm505-17l-8 10 1 104-98-33-12 5c-56 20.8-115.7 22.5-171 7l-.2-.1A367.31 367.31 0 00729 676c76.4-105.3 88.8-237.6 44.4-350.4l.6.4c23 16.5 44.1 37.1 62 62 72.6 99.6 68.5 235.2-8 330z"></path><path d="M433 421c-23.1 0-41 17.9-41 40s17.9 40 41 40c21.1 0 39-17.9 39-40s-17.9-40-39-40z"></path></svg></span></span>讨论</a></div><div class="rc-footer-item"><a href="/docs/community/join-us-cn#加入我们"><span class="rc-footer-item-icon"><span role="img" aria-label="usergroup-add" class="anticon anticon-usergroup-add"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="usergroup-add" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M892 772h-80v-80c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v80h-80c-4.4 0-8 3.6-8 8v48c0 4.4 3.6 8 8 8h80v80c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8v-80h80c4.4 0 8-3.6 8-8v-48c0-4.4-3.6-8-8-8zM373.5 498.4c-.9-8.7-1.4-17.5-1.4-26.4 0-15.9 1.5-31.4 4.3-46.5.7-3.6-1.2-7.3-4.5-8.8-13.6-6.1-26.1-14.5-36.9-25.1a127.54 127.54 0 01-38.7-95.4c.9-32.1 13.8-62.6 36.3-85.6 24.7-25.3 57.9-39.1 93.2-38.7 31.9.3 62.7 12.6 86 34.4 7.9 7.4 14.7 15.6 20.4 24.4 2 3.1 5.9 4.4 9.3 3.2 17.6-6.1 36.2-10.4 55.3-12.4 5.6-.6 8.8-6.6 6.3-11.6-32.5-64.3-98.9-108.7-175.7-109.9-110.8-1.7-203.2 89.2-203.2 200 0 62.8 28.9 118.8 74.2 155.5-31.8 14.7-61.1 35-86.5 60.4-54.8 54.7-85.8 126.9-87.8 204a8 8 0 008 8.2h56.1c4.3 0 7.9-3.4 8-7.7 1.9-58 25.4-112.3 66.7-153.5 29.4-29.4 65.4-49.8 104.7-59.7 3.8-1.1 6.4-4.8 5.9-8.8zM824 472c0-109.4-87.9-198.3-196.9-200C516.3 270.3 424 361.2 424 472c0 62.8 29 118.8 74.2 155.5a300.95 300.95 0 00-86.4 60.4C357 742.6 326 814.8 324 891.8a8 8 0 008 8.2h56c4.3 0 7.9-3.4 8-7.7 1.9-58 25.4-112.3 66.7-153.5C505.8 695.7 563 672 624 672c110.4 0 200-89.5 200-200zm-109.5 90.5C690.3 586.7 658.2 600 624 600s-66.3-13.3-90.5-37.5a127.26 127.26 0 01-37.5-91.8c.3-32.8 13.4-64.5 36.3-88 24-24.6 56.1-38.3 90.4-38.7 33.9-.3 66.8 12.9 91 36.6 24.8 24.3 38.4 56.8 38.4 91.4-.1 34.2-13.4 66.3-37.6 90.5z"></path></svg></span></span>加入我们</a></div></div><div class="rc-footer-column"><h2>帮助</h2><div class="rc-footer-item"><a href="https://github.com/micro-in-cn/questions" target="_blank"><span class="rc-footer-item-icon"><span role="img" aria-label="github" class="anticon anticon-github"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="github" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M511.6 76.3C264.3 76.2 64 276.4 64 523.5 64 718.9 189.3 885 363.8 946c23.5 5.9 19.9-10.8 19.9-22.2v-77.5c-135.7 15.9-141.2-73.9-150.3-88.9C215 726 171.5 718 184.5 703c30.9-15.9 62.4 4 98.9 57.9 26.4 39.1 77.9 32.5 104 26 5.7-23.5 17.9-44.5 34.7-60.8-140.6-25.2-199.2-111-199.2-213 0-49.5 16.3-95 48.3-131.7-20.4-60.5 1.9-112.3 4.9-120 58.1-5.2 118.5 41.6 123.2 45.3 33-8.9 70.7-13.6 112.9-13.6 42.4 0 80.2 4.9 113.5 13.9 11.3-8.6 67.3-48.8 121.3-43.9 2.9 7.7 24.7 58.3 5.5 118 32.4 36.8 48.9 82.7 48.9 132.3 0 102.2-59 188.1-200 212.9a127.5 127.5 0 0138.1 91v112.5c.8 9 0 17.9 15 17.9 177.1-59.7 304.6-227 304.6-424.1 0-247.2-200.4-447.3-447.5-447.3z"></path></svg></span></span>GitHub</a></div></div><div class="rc-footer-column"><h2><span class="rc-footer-column-icon"><img src="https://gw.alipayobjects.com/zos/rmsportal/nBVXkrFdWHxbZlmMbsaH.svg" alt="Ant XTech"/></span>更多产品</h2><div class="rc-footer-item"><a href="https://yuque.com" target="_blank"><span class="rc-footer-item-icon"><img src="https://gw.alipayobjects.com/zos/rmsportal/XuVpGqBFxXplzvLjJBZB.svg" alt="yuque"/></span>语雀</a><span class="rc-footer-item-separator">-</span><span class="rc-footer-item-description">知识创作与分享工具</span></div></div></section></section><section class="rc-footer-bottom"><div class="rc-footer-bottom-container">Made with <span style="color:#fff">❤</span> by<!-- --> <a target="_blank" rel="noopener noreferrer" href="https://xtech.antfin.com">XTech</a></div></section></footer></div></div></div></div>
    </div>
    
    <script src="/index-c81a5111.js"></script>
    
    <!-- Global Site Tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-72788897-1"></script>
    <script>
      if (!location.port) {
        // Enable Google Analytics
        window.dataLayer = window.dataLayer || [];
        function gtag() {
          dataLayer.push(arguments);
        }
        gtag('js', new Date());
        gtag('config', 'UA-72788897-1');
      }
    </script>
    <!-- Hotjar Tracking Code for microhq.cn -->
    <script>
      (function (h, o, t, j, a, r) {
        h.hj =
          h.hj ||
          function () {
            (h.hj.q = h.hj.q || []).push(arguments);
          };
        h._hjSettings = { hjid: 1779539, hjsv: 6 };
        a = o.getElementsByTagName('head')[0];
        r = o.createElement('script');
        r.async = 1;
        r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
        a.appendChild(r);
      })(window, document, 'https://static.hotjar.com/c/hotjar-', '.js?sv=');
    </script>
  </body>
</html>
